<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/login.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/log2.jpg" alt="">
        <a href="blog_list.html" class="title">博客系统主页</a>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="myblog_list.html">主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="login.html">登录</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="login-container">
        <!-- 中间的注册框 -->
        <div class="reg-dialog">
            <h3>注册</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="row">
                <span>确认密码</span>
                <input type="password" id="password2">
            </div>
            <div class="row">
                <span>码云地址</span>
                <input type="text" id="gitaddress">
            </div>
            <div class="row">
                <span>手机号</span>
                <input type="text" id="phonenumber">
            </div>
            <div class="row" id="reg">
                <button id="submit" onclick="mysub()">注册</button>
            </div>
        </div>
    </div>
</body>
<script>
    //提交注册事件
    function mysub() {
        //1.非空校验
        var username = jQuery('#username');
        var password = jQuery('#password');
        var password2 = jQuery('#password2');
        var gitaddress = jQuery('#gitaddress');
        var phonenumber = jQuery('#phonenumber');
        if (username.val() == '') {
            alert("用户名不能为空");
            //将用户的光标设置到此行
            username.focus();
            return;
        }
        if (password.val() == '') {
            alert("密码不能为空")
            //将用户的光标设置到此行
            password.focus();
            return;
        }
        if (password2.val() == '') {
            alert("确认密码不能为空");
            //将用户的光标设置到此行
            password2.focus();
            return;
        }
        if (phonenumber.val() == '') {
            alert("电话不能为空,用于修改/找回密码!");
            //将用户的光标设置到此行
            phonenumber.focus();
            return;
        }
        if (phonenumber.val().length < 11 || phonenumber.val().length > 11) {
            alert("非法号码,请重新填写!");
            //将用户的光标设置到此行
            phonenumber.focus();
            return;
        }
        //2.判断两次密码是否一致
        if (password.val() != password2.val()) {
            alert("两次输入的密码不一致!")
            return;
        }
        //3.ajax提交请求 注册结果
        jQuery.ajax({
            type: 'post',
            url: '/user/reg',
            data: {
                "username": username.val(),
                "password": password.val(),
                "password2": password2.val(),
                "gitaddress": gitaddress.val(),
                "phonenumber": phonenumber.val()
            },
            success: function (result) {
                //响应的结果
                if (result != null && result.code == 200 && result.data == 1) {
                    //执行成功
                    if (confirm("注册成功！跳转到登录页面")) {
                        location.href = '/login.html'
                    }
                } else {
                    alert("抱歉,注册失败,用户名已存在!")
                }
            }
        })

    }
</script>

</html>